/*****************************************************
PROJECT: BRIDGE C2 SCRAP
JOB:
REQUESTOR:Rachel Gold
JIRA #:
ANALYST: Jean O'Malley
DATE: 8/30/2022 5/4/2023
VERSION: 
LAST UPDATED: 
PEER REVIEWER: 

PROJECT DESCRIPTION: Use ASCEND approach to compare CRC, CVC and 
BC cancer prevention services in patients with
	No SDH Screening
	Documented Need
	Documented No Need
Stratified by SDH need: Food, Housing, Transportation
STUDY PERIOD: 7/1/2016-2/29/2020		

DETAILS (this file): change reference group to documented no need
CVC Outcome variables by SDH
	UTD at index 
	Months UTD over observation period
	Among Patients due 12 months before obs end, Receipt of screening order within 12 months
	Among patients with order, 
			receipt of screen while due(Poisson with months after due as offset)
Table 2.Columns Food Insecurity: Need/ No Need/ Not documented
Table 3 Columns Housing Insecurity: Need/ No Need/ Not documented
Table 4 Columns Transportation Insecurity: Need/ No Need/ Not documented
		

INPUT DATA:
SCRAP.CVC_outcomes;

OUTPUT DATA:
SCRAP.CVC_Table2_Food
SCRAP.CVC_Table3_Housing
SCRAP.CVC_Table4_Transportation


SECTIONS:
A. Dynamic variables and analysis file
B. Full cohort variables
	B1. UTD at index 
	B2. Months UTD over observation period
	B3. Visits in year following Index (includes index visit
	B4. Primary Care Visits in year following Index (includes index visit

C.Among Patients due 12 months before obs end, 
	C1. Receipt of any screening order within 12 months

D.Among patients with order, 
	D1. receipt of screen while due(Poisson with months after due as offset)


****************************************************/
options compress=yes reuse=yes spool;
libname SCRAP "e:\\sasroot\BRIDGE C2\SCRAP\";
libname RD odbc noprompt="dsn=research_dev;Trusted_Connection=yes" bulkload=yes;
/****************************************************/

/***************************************************
A. Dynamic variables and analysis file
***************************************************/
%let cancer=CVC;
%Let covariates=race_ethnic2 language_c  FPL_cat2 age_cat payor_type_research visit_cat;
%Let class=delivery_site_id age_cat race_ethnic2 language_c  FPL_cat2 visit_cat payor_type_research;


data analysis;
set SCRAP.&cancer._analysis;
sdh_trans=sdh_transportation;
run;
proc sort data=analysis;
by delivery_site_id;
run;


/************************************************
B. Full cohort variables
************************************************/
%macro tables;
/*	B1. UTD at index */


	proc genmod data=analysis ;
	class &class &sdh /ref=first;
	model due=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL om;
	ods output LSmeans=due_&sdh lsmestimates=due_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=due_&sdh out=var1_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var1_&sdh(keep=Outcome &sdh prob_CI);
	set var1_&sdh;
	length Outcome $30;
	format mu2 lower2 upper2 4.1;
	Outcome="Up to Date &cancer screening";
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=due_diffs_&sdh out=diff1_&sdh (keep= Probz label Mu Lowermu Uppermu);
	by label;
	run;

	data diff1_&sdh (keep=Outcome &sdh reference RR_CI probz);
	set diff1_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Up to Date &cancer screening";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	data var1_&sdh;
	merge var1_&sdh diff1_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete due_diffs_&sdh diff1_&sdh  due_&sdh;
	quit;


/*	B3. Months UTD over observation period*/

	proc genmod data=analysis ;
	class &class &sdh /ref=first;
	model months_utd=&sdh &covariates/link=log d=negbin type3 offset=months_offset;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=mUTD_&sdh lsmestimates=mUTD_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=mUTD_&sdh out=var3_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var3_&sdh(keep=Outcome &sdh prob_CI);
	set var3_&sdh;
	length Outcome $30;
	Outcome="Percent Months UTD";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=mUTD_diffs_&sdh out=diff3_&sdh (keep= Probz label mu Lowermu Uppermu);
	by label;
	run;

	data diff3_&sdh (keep=Outcome &sdh reference rr_CI probz);
	set diff3_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lowermu uppermu 4.2;
	Outcome="Percent Months UTD";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	data var3_&sdh;
	merge var3_&sdh diff3_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete mUTD_diffs_&sdh diff3_&sdh mUTD_diffs_&sdh mUTD_&sdh;
	quit;

/*	B4. Visits in year after index*/


	proc genmod data=analysis ;
	class &class &sdh /ref=first;
	model visits_first_yr=&sdh race_ethnic2 language_c  FPL_cat2 
	age_cat payor_type_research /link=log d=negbin type3 ;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=visits_&sdh lsmestimates=visits_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=visits_&sdh out=var32_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var32_&sdh(keep=Outcome &sdh prob_CI);
	set var32_&sdh;
	length Outcome $30;
	Outcome="Visits in Year after Index";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=visits_diffs_&sdh out=diff32_&sdh (keep= Probz label mu Lowermu Uppermu);
	by label;
	run;

	data diff32_&sdh (keep=Outcome &sdh reference rr_CI probz);
	set diff32_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lowermu uppermu 4.2;
	Outcome="Visits in Year after Index";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	data var32_&sdh;
	merge var32_&sdh diff32_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete visits_diffs_&sdh diff32_&sdh visits_diffs_&sdh visits_&sdh;
	quit;

/*	B5. PC Visits in year after index*/


	proc genmod data=analysis ;
	class &class &sdh /ref=first;
	model PC_visits_first_yr=&sdh race_ethnic2 language_c  FPL_cat2 
	age_cat payor_type_research /link=log d=negbin type3 ;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL OM;
	ods output LSmeans=PCvisits_&sdh lsmestimates=PCvisits_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=PCvisits_&sdh out=var33_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var33_&sdh(keep=Outcome &sdh prob_CI);
	set var33_&sdh;
	length Outcome $30;
	Outcome="PC Visits in Year after Index";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;

	proc sort data=PCvisits_diffs_&sdh out=diff33_&sdh (keep= Probz label mu Lowermu Uppermu);
	by label;
	run;

	data diff33_&sdh (keep=Outcome &sdh reference rr_CI probz);
	set diff33_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lowermu uppermu 4.2;
	Outcome="PC Visits in Year after Index";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(MU,0.01);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMU,0.01);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMU,0.01);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	data var33_&sdh;
	merge var33_&sdh diff33_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete visits_diffs_&sdh diff33_&sdh visits_diffs_&sdh visits_&sdh;
	quit;


/********************************************************
/*C.Among Patients due 12 months before obs end, */
*********************************************************/;
data analysis2;
set analysis;
where one_year_due^=. and observation_end>=one_year_due
and time_to_screen < months_in_study;
run;
proc freq data=analysis2;
tables SDH_Transportation;
run;	

/*	C1.Receipt of any screening order within 12 months*/


	proc genmod data=analysis2 descending;
	class &class &sdh /ref=first;
	model order_1y=&sdh &covariates/link=log d=binomial type3;
	repeated subject=delivery_site_id/type=exch;
	LSmeans &sdh/ilink CL diff OM;
	ods output LSmeans=order_&sdh lsmestimates=order_diffs_&sdh;
	lsmestimate &sdh '1 vs 2' -1 0 1/cl  om ilink;
	lsmestimate &sdh '3 vs 2' -1 1 0 /cl   om ilink;
	run;

	proc sort data=order_&sdh out=var4_&sdh (keep=&sdh Mu LowerMu UpperMu);
	by &sdh;
	run;
	data var4_&sdh(keep=Outcome &sdh prob_CI);
	set var4_&sdh;
	length Outcome $30;
	Outcome="Order within 1 year of due";
	format mu2 lower2 upper2 4.1;
	mu2=round(MU*100,0.1);
	mu_txt=put(mu2,4.1); 
	lower2=round(lowerMU*100,0.1);
	lower_txt=put(lower2,4.1);
	upper2=round(upperMU*100,0.1);
	upper_txt=put(upper2,4.1);
	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;
	proc sort data=order_diffs_&sdh out=diff4_&sdh (keep= Probz  label Mu LowerMu UpperMu);
	by label;
	run;

	data diff4_&sdh (keep=Outcome &sdh reference RR_CI probz);
	set diff4_&sdh;
	length Outcome $30 &sdh reference $20;
	format mu2 lower2 upper2 4.2;
	Outcome="Order within 1 year of due";
	reference='2_Negative Screen';
	if label='1 vs 2' then &SDH= '1_Positive Screen';
	if label='3 vs 2' then &SDH= '3_Never Screened';
	mu2=round(mu,0.1);
	mu_txt=put(mu2,4.2); 
	lower2=round(lowerMu,0.1);
	lower_txt=put(lower2,4.2);
	upper2=round(upperMu,0.1);
	upper_txt=put(upper2,4.2);
	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');
	run;


	data var4_&sdh;
	merge var4_&sdh diff4_&sdh;
	by outcome &sdh;
	run;


	proc datasets library=work;
	delete order_diffs_&sdh diff4_&sdh  order_&sdh;
	quit;




/*Among patients with order*/
/*	D2. receipt of any screen*/
	data analysis3;
	set analysis2;
	where order_1y=1;
	run;


	proc freq data=analysis3;
	tables sdh_transportation;
	run;
	proc freq data=analysis3;
	tables (sdh_food sdh_housing sdh_transportation)*screen_1yr/nocol nopercent;
	run;

/**/
/*	proc genmod data=analysis3 descending;*/
/*	class &class &sdh /ref=first;*/
/*	model screen_1yr=&sdh &covariates/link=log d=binomial type3;*/
/*	repeated subject=delivery_site_id/type=exch;*/
/*	LSmeans &sdh/ilink CL OM;*/
/*	ods output LSmeans=screen_&sdh lsmestimates=screen_diffs_&sdh;*/
/*	lsmestimate &sdh '2 vs 1' 1 0 -1/cl ilink OM;*/
/*	lsmestimate &sdh '3 vs 1' 0 1 -1/cl ilink OM;*/
/*	run;*/
/**/
/*	proc sort data=screen_&sdh out=var6_&sdh (keep=&sdh Mu LowerMu UpperMu);*/
/*	by &sdh;*/
/*	run;*/
/*	data var6_&sdh(keep=Outcome &sdh prob_CI);*/
/*	set var6_&sdh;*/
/*	length Outcome $30;*/
/*	Outcome="Completed CVC Screen";*/
/*	mu2=round(Mu*100,0.1);*/
/*	mu_txt=put(mu2,4.1); */
/*	lower2=round(lowerMu*100,0.1);*/
/*	lower_txt=put(lower2,4.1);*/
/*	upper2=round(upperMu*100,0.1);*/
/*	upper_txt=put(upper2,4.1);*/
/*	prob_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');*/
/*	run;*/
/*	proc sort data=screen_diffs_&sdh out=diff6_&sdh (keep= label probz Mu LowerMu UpperMu);*/
/*	by label;*/
/*	run;*/
/**/
/*	data diff6_&sdh (keep=Outcome &sdh reference RR_CI probz);*/
/*	set diff6_&sdh;*/
/*	length Outcome $30 &sdh reference $20;*/
/*	Outcome="Completed CVC Screen";*/
/*	format mu2 lower2 upper2 4.2;*/
/*	reference='1_Positive Screen';*/
/*	if label='2 vs 1' then &SDH= '2_Negative Screen';*/
/*	if label='3 vs 1' then &SDH= '3_Never Screened';*/
/*	mu2=round(mu,0.1);*/
/*	mu_txt=put(mu2,4.2); */
/*	lower2=round(lowermu,0.1);*/
/*	lower_txt=put(lower2,4.2);*/
/*	upper2=round(uppermu,0.1);*/
/*	upper_txt=put(upper2,4.2);*/
/*	RR_CI=cat(mu_txt,' (',lower_txt,', ',upper_txt,')');*/
/*	run;*/
/**/
/*	data var6_&sdh;*/
/*	merge var6_&sdh diff6_&sdh;*/
/*	by outcome &sdh;*/
/*	run;*/
/**/
/*	proc datasets library=work;*/
/*	delete screen_diffs_&sdh diff6_&sdh  screen_&sdh;*/
/*	quit;*/


;
	data Table_&sdh;
	set var1_&sdh 
		var3_&sdh
		var4_&sdh
/*		var6_&sdh*/
		var32_&sdh
		var33_&sdh
;
	run;

*reorder columns;
proc sql;
create table tableb as
select outcome,
&sdh,
prob_CI,
reference,
rr_ci,
probz
from Table_&sdh;
quit;

data scrap.Table_&sdh.&cancer;
set tableb;
run;
%mend;

%let SDH=SDH_Food; 
%tables;

%let SDH=SDH_Housing;
%tables;

%Let SDH=SDH_Trans;
%tables;





